#ifndef CHANNEL_H
#define CHANNEL_H

#include "systemc.h"
#include "random.h"

template <typename T>
class async_channel_in_if: virtual public sc_interface
{
public:
	virtual void read(T&) = 0;
};

template <typename T>
class async_channel_out_if: virtual public sc_interface
{
public:
	virtual void write(T) = 0;
};

template <typename T>
class async_channel: public async_channel_in_if<T>, public async_channel_out_if<T>, public sc_prim_channel
{
private:
	sc_semaphore req,ack;
// 	sc_event req,ack;

public:
	T data;
	virtual void write(T);
	virtual void read(T&);

//-------------Construtor-----------------------------------------------------//
	async_channel():req(0),ack(0){}
//----------------------------------------------------------------------------//
};

template <typename T>
void async_channel<T>::write(T in)
{
	wait(RandomValue(1,5), SC_NS);
	data = in;
	req.post();
	ack.wait();
}

template <typename T>
void async_channel<T>::read(T& out)
{
	wait(RandomValue(1,5), SC_NS);
	req.wait();
	out = data;
	ack.post();
}


#endif